﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Marmat.FrameWork;
using MethodBoundaryAspect.Fody.Attributes;

namespace Marmat.Template.WinForm
{

    /// <summary>
    /// 权限注解，只有管理员才能执行该函数。
    /// </summary>
    public class AdminPermissionAttribute : OnMethodBoundaryAspect, Marmat.Template.WinForm.IPermissionAttribute, Marmat.Foundation.IProvider
    {
        /// <summary>
        /// 初始化 Marmat.Template.WinForm.AdminPermissionAttribute 类的新实例。
        /// </summary>
        public AdminPermissionAttribute()
        {
        }

        /// <summary>
        /// 进入方法前执行。
        /// </summary>
        /// <param name="arg"></param>
        public override void OnEntry(MethodExecutionArgs arg)
        {
            if (this.Verify())
                base.OnEntry(arg);
            else
                arg.FlowBehavior = FlowBehavior.Return;
        }

        /// <summary>
        /// 鉴权
        /// </summary>
        /// <returns></returns>
        public bool Verify()
        {
            Marmat.Foundation.IPermission permission = SystemCommand.GetService<Marmat.Foundation.IPermission>();
            return permission is null || permission.IsAdmin();
        }
    }
}
